package jira;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.ObjectMapper;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.log4j.Logger;
import java.io.IOException;

public class JiraRestApiUtil {
    private static Logger log = Logger.getLogger(JiraRestApiUtil.class.getClass());
    //登录用户名
    private static String username="admin";
    //登录密码
    private static String password="1234";
    //jira访问地址
    private static String jiraurl="http://localhost:8888";

    /**
     * jira get请求
     * @param url
     * @return
     */
    public static String GetAction(String url)  {
        String result=null;
        try {
            log.info("GET请求开始：");
            HttpResponse<JsonNode> response = Unirest.get(jiraurl+url)
                    .basicAuth(username, password)
                    .header("Accept", "application/json")
                    .asJson();
            result=response.getBody().toString();
            log.info("GET请求结束");
        } catch (UnirestException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * jira POST请求
     * @param url
     * @param payload
     * @return
     */
    public static String PostAction(String url, ObjectNode payload){
        String result=null;
        try{
            Unirest.setObjectMapper(new ObjectMapper() {
                private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
                        = new com.fasterxml.jackson.databind.ObjectMapper();
                public <T> T readValue(String value, Class<T> valueType) {
                    try {
                        return jacksonObjectMapper.readValue(value, valueType);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
                public String writeValue(Object value) {
                    try {
                        return jacksonObjectMapper.writeValueAsString(value);
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                }
            });

            HttpResponse<JsonNode> response = Unirest.post(jiraurl+url)
                    .basicAuth(username, password)
                    .header("Accept", "application/json")
                    .header("Content-Type", "application/json")
                    .body(payload)
                    .asJson();
            result=response.getBody().toString();
        }catch (Exception e){
            log.error(e.toString());
            StackTraceElement[] staList=e.getStackTrace();
            StringBuilder errorStackTrace=new StringBuilder();
            for(int i=0;i<staList.length;i++){
                log.error(staList[i].toString());
            }

        }
        return result;
    }
}
